package views.fee.seeker;

import org.intellij.lang.annotations.Language;
import tools.DateChooser;
import tools.Hint;
import utils.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class FeeSeekerAddFrame extends JFrame {
    int identity, feeid;
    ImageIcon imageIcon = new ImageIcon("src/images/fee.png");
    private JPanel panBtn = new JPanel();
    private JPanel panLab = new JPanel();
    private JLabel jL_identity = new JLabel("求职者身份证号：");
    private JLabel jL_money = new JLabel("金额：");
    private JLabel jL_date = new JLabel("交费日期：");
    private JLabel jL_remark = new JLabel("备注：");
    private JComboBox<String> jC_identity = new JComboBox<String>();
    private JTextField jT_money = new JTextField();
    private JTextField jT_date = new JTextField();
    private JComboBox<String> jC_remark = new JComboBox<String>();
    private JButton btn_add = new JButton("添加");
    private JButton btn_reset = new JButton("重置");
    private JLabel jL_title = new JLabel();
    DateChooser dateChooser = DateChooser.getInstance("yyyy-MM-dd");

    public FeeSeekerAddFrame() {
        setTitle("添加求职者交费信息");
        setSize(400, 300);
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        setResizable(false); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        jL_title.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        jL_title.setHorizontalAlignment(JLabel.CENTER);
        jL_title.setForeground(new Color(255, 51, 51));
        jL_title.setText("添 加 求 职 者 交 费 信 息");

        jL_identity.setSize(100, 80);
        jL_identity.setLocation(50, 5);
        jL_identity.setHorizontalAlignment(JLabel.RIGHT);

        jL_money.setSize(100, 80);
        jL_money.setLocation(50, 40);
        jL_money.setHorizontalAlignment(JLabel.RIGHT);

        jL_date.setSize(100, 80);
        jL_date.setLocation(50, 80);
        jL_date.setHorizontalAlignment(JLabel.RIGHT);

        jL_remark.setSize(100, 80);
        jL_remark.setLocation(50, 120);
        jL_remark.setHorizontalAlignment(JLabel.RIGHT);

        /*文本框位置*/
        try {
            for (int k = 0; k < getidentity().size(); k++) {
                jC_identity.addItem(getidentity().get(k));
            }
            jC_identity.setSize(150, 25);
            jC_identity.setLocation(160, 33);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        jT_money.setSize(150, 25);
        jT_money.setLocation(160, 68);
        String hint_person = "输入交费金额";
        jT_money.addFocusListener(new Hint(jT_money, hint_person));

        jT_date.setSize(150, 25);
        jT_date.setLocation(160, 108);
        String hint_phone = "输入交费日期";
        jT_date.addFocusListener(new Hint(jT_date, hint_phone));
        dateChooser.register(jT_date);

        try {
            for (int k = 0; k < getfeerepresent_name().size(); k++) {
                jC_remark.addItem(getfeerepresent_name().get(k));
            }
            jC_remark.setSize(150, 25);
            jC_remark.setLocation(160, 148);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        panBtn.add(btn_add);
        panBtn.add(btn_reset);
        panLab.setLayout(null);

        // panLab.add(jLabel1);
        panLab.add(jL_identity);
        panLab.add(jL_money);
        panLab.add(jL_date);
        panLab.add(jL_remark);

        panLab.add(jC_identity);
        panLab.add(jT_money);
        panLab.add(jT_date);
        panLab.add(jC_remark);

        add(jL_title, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        btn_add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int seekeridentity = Integer.parseInt(jC_identity.getSelectedItem().toString().trim());
                try {
                    identity = seleseekerid(seekeridentity);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String money = jT_money.getText().trim();
                String date = jT_date.getText().trim();
                String fee_remark = jC_remark.getSelectedItem().toString();
                try {
                    feeid = selefeerepresent_id(fee_remark);
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                if (money.equals(hint_person) || date.equals(hint_phone)) {
                    ImageIcon warningImage = new ImageIcon("src/images/warning.png");
                    warningImage.setImage(warningImage.getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
                    JOptionPane.showMessageDialog(null, "请输入内容", "错误", JOptionPane.WARNING_MESSAGE, warningImage);
                } else {
                    Statement stmt = DBUtil.getStatement();
                    @Language("MySQL")
                    String sql2 = "insert into fee_seeker(job_seeker_id, amount, paydate, Seeker_remark) values( "
                            + identity
                            + " , '"
                            + money
                            + "' , '"
                            + date
                            + "' , "
                            + feeid
                            + " );";
                    try {
                        stmt.executeUpdate(sql2);
                        ImageIcon okImage = new ImageIcon("src/images/OK.png");
                        okImage.setImage(okImage.getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
                        JOptionPane.showMessageDialog(null, "添加成功。", "成功", JOptionPane.INFORMATION_MESSAGE, okImage);
                        dispose();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        btn_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                jC_identity.setSelectedItem(-1);
                jT_money.setText("");
                jT_date.setText("");
                jC_remark.setSelectedItem(-1);
            }
        });
    }

    public Vector<String> getfeerepresent_name() throws SQLException {
        Vector<String> feerepresent_name = new Vector<String>();
        String sql = "SELECT express from fee_representation";
        Statement stmt = DBUtil.getStatement();
        ResultSet rs1 = stmt.executeQuery(sql);
        while (rs1.next()) {
            feerepresent_name.add(rs1.getString("express"));
        }
        DBUtil.closeMySQL();// 关闭连接
        return feerepresent_name;
    }

    public Vector<String> getidentity() throws SQLException {
        Vector<String> identityNum = new Vector<String>();
        String sql = "SELECT identityNum from job_seeker ORDER BY seeker_id";
        Statement stmt = DBUtil.getStatement();
        ResultSet rs1 = stmt.executeQuery(sql);
        while (rs1.next()) {
            identityNum.add(rs1.getString("identityNum"));
        }
        DBUtil.closeMySQL();// 关闭连接
        return identityNum;
    }

    public int selefeerepresent_id(String name) throws SQLException {
        @Language("MySQL")
        String sql = "SELECT id from fee_representation WHERE express='" + name + "';";
        int count = 0;
        Statement pr = DBUtil.getStatement();
        ResultSet rs = pr.executeQuery(sql);
        while (rs.next()) { // 遍历数据库的数据
            count = rs.getInt(1);
        }
        DBUtil.closeMySQL();// 关闭连接
        return count;
    }

    public int seleseekerid(int identity) throws SQLException {
        @Language("MySQL")
        String sql = "SELECT seeker_id from job_seeker WHERE identityNum=" + identity + ";";
        int count = 0;
        Statement pr = DBUtil.getStatement();
        ResultSet rs = pr.executeQuery(sql);
        while (rs.next()) { // 遍历数据库的数据
            count = rs.getInt(1);
        }
        DBUtil.closeMySQL();// 关闭连接
        return count;
    }
}
